(function($){
	$.fn.chargeListInit = function(){
		var $this = $(this);
		var selectRow = null;
		var selectIndex = null;
		// 0:查询选择  1:新增选择
		var type = 0;
		//新增
		var chargeDialog = $('#chargeDialog',$this);
		//选择客户
		var selectDialog= $('#selectDialog',$this);

		//查询
		$('#search',$this).click(function(){
			var queryContent = $('.queryContent',$this);
			var customerID = $('#customerID',queryContent).val();
			var beginDate = $('#beginDate',queryContent).val();
			var endDate = $('#endDate',queryContent).val();
			var content = {customerID:customerID,beginDate:beginDate,endDate:endDate};
			var url = 'cardCharge!select.action';
			$('#chargeList',$this).datagrid({
				url:url,
				queryParams:content,
				pageNumber:1
			});
		})
		//重置
		$('#reset',$this).click(function(){
			var queryContent = $('.queryContent',$this);
			$('#customerID',queryContent).val('');
			$('#customerName',queryContent).val('');
			$('#beginDate',queryContent).val('');
			$('#endDate',queryContent).val('');
		})
		//新增
		var onAdd = function(){
			$('#selectcustomerSearch',chargeDialog).show();
			$(chargeDialog).dialog('open');
		}
		//删除
		var onDelete = function(){
			if(selectRow==null){
				$.messager.alert("提示","请选择数据行","warning");
				return;
			}
			$.messager.confirm('确认', '确定要删除吗?', function(r){
				if (r){
					var url = 'cardCharge!delete.action';
					var content = {chargeID:selectRow.chargeID};
					asyncCallService(url,content,function(result){
						if(result.success){
							$.messager.alert("提示","删除成功","info",onReload);
						}else{
							$.messager.alert("提示",result.msg,"warning");
						}
					})
				}
			});
		}
		//修改
		var onUpdate = function(){
			$('#selectcustomerSearch',chargeDialog).hide();
			$('#chargeForm',chargeDialog).form('load',selectRow);
			$(chargeDialog).dialog('open');
		}
		//刷新
		var onReload = function(){
			$('#chargeList',$this).datagrid('reload');
		}
		//加载查询页面列表
		$('#chargeList',$this).datagrid({
			singleSelect:true,
			fit:true,
			method:"POST",
			nowrap:true,
			striped: true,
			collapsible:true,
			rownumbers:true,
			pagination:true,
			pageSize:30,
			toolbar:[	
						{id:'add',text:'添加',iconCls:'icon-add',handler:function(){onAdd()}},
						{id:'delete',text:'删除',iconCls:'icon-remove',handler:function(){onDelete()}},
						{id:'update',text:'修改',iconCls:'icon-edit',handler:function(){onUpdate()}},
						{id:'reload',text:'刷新',iconCls:'icon-reload',handler:function(){onReload()}}
					],
			columns:[[
				{field:'chargeID',hidden:true},
				{field:'customerID',hidden:true},
				{field:'customerCode',title:'客户编号',width:100,align:"center"},
				{field:'customerName',title:'客户名称',width:200,align:"center"},
				{field:'cardCode',title:'充值卡号',width:100,align:"center"},
				{field:'chargeDate',title:'充值日期',width:100,align:"center"},
				{field:'actualAmount',title:'实充金额',width:100,align:"center"},
				{field:'freeAmount',title:'赠送金额',width:100,align:"center"},
				{field:'totalAmount',title:'合计金额',width:100,align:"center",
					formatter:function(value,row,index){
						var freeAmount = row.freeAmount;
						if(freeAmount==null){
							freeAmount=0;
						}
						return parseFloat(row.actualAmount)+parseFloat(freeAmount);
				}},
				{field:'userName',title:'经办人',width:100,align:"center"}
			]],
			onClickRow:function(rowIndex, rowData){
				selectRow = rowData;
				selectIndex = rowIndex;
			},
			onDblClickRow:function(rowIndex,rowData){
				onUpdate();
			},
			onLoadSuccess:function(){
				selectRow = null;
		 		selectIndex = null;
			}
		});
		
		$('#customerSearch',$this).click(function(){
			type=0;
			$(selectDialog).dialog('open');
		})
		
		$('#selectcustomerSearch',chargeDialog).click(function(){
			type=1;
			$(selectDialog).dialog('open');
		})
		//保存
		var onSave = function(){
			var customerID = $('#customerID',chargeDialog).val();
			if($.trim(customerID)==''){
				$.messager.alert('提示','客户不能为空','warning');
				return;
			}
			var chargeDate = $('#chargeDate',chargeDialog).val();
			if($.trim(chargeDate)==''){
				$.messager.alert('提示','充值日期不能为空','warning');
				return;
			}
			var actualAmount = $('#actualAmount',chargeDialog).val();
			if($.trim(actualAmount)==''||parseFloat(actualAmount)==0){
				$.messager.alert('提示','充值金额不能为空','warning');
				return;
			}
			var url = null;
			if($('#chargeID',chargeDialog).val()!=''){
				url= 'cardCharge!update.action';
			}else{
				url= 'cardCharge!add.action';
			}
			$('#chargeForm',chargeDialog).ajaxSubmit({
				url:url,
				type:'post',
				dataType:'json',
				success:function(result){	
					if(result.success){
						var fn = function(){
							onExit();
							$('#search',$this).click();
						}
						$.messager.alert('提示','保存成功','info',fn);
					}else{
						$.messager.alert('提示',result.msg,"warning");
					}
				}
			});
		}
		//退出
		var onExit = function(){
			$('#chargeForm',chargeDialog).form('clear');
			$(chargeDialog).dialog('close');
		}
		
		$(chargeDialog).dialog({  
		    title: '充值',  
		    width:400,
		    height:250,
		    closed: true,  
		    cache: false,  
		    modal: true,
		    closable:false,
		    toolbar:[{text:'保存',iconCls:'icon-save',handler:function(){onSave()}},'-',
		    		 {text:'退出',iconCls:'icon-exit',handler:function(){onExit()}}]
		});
		
		var onSelect = function(){
			var row = $('#customerList',selectDialog).datagrid('getSelected');
			if(row==null){
				$.messager.alert("提示","请选择数据行","warning");
				return;
			}
			//查询选择
			if(type==0){
				var queryContent = $('.queryContent',$this);
				$('#customerID',queryContent).val(row.customerID);
				$('#customerName',queryContent).val(row.customerName);
			//新增选择
			}else if(type==1){
				$('#customerID',chargeDialog).val(row.customerID);
				$('#customerName',chargeDialog).val(row.customerName);
			}
			onSelectExit();
		}
		
		var onSelectExit = function(){
			$(selectDialog).dialog('close');
		}
		
		$(selectDialog).dialog({  
		    title: '选择客户',  
		    width:900,
		    height:550,
		    closed: true,  
		    cache: false,  
		    modal: true,
		    closable:false,
		    toolbar:[{text:'选择',iconCls:'icon-ok',handler:function(){onSelect()}},'-',
		    		 {text:'退出',iconCls:'icon-exit',handler:function(){onSelectExit()}}]
		});
		//选择页面查询
		$('#selectSearch',selectDialog).click(function(){
			var customerCode = $('#customerCode',selectDialog).val();
			var customerName = $('#customerName',selectDialog).val();
			
			var content = {customerCode:customerCode,customerName:customerName};
			
			$('#customerList',selectDialog).datagrid({
				url:"customer!select.action",
				queryParams:content,
				pageNumber:1
			});
		})
		//加载客户列表
		$('#customerList',selectDialog).datagrid({
			singleSelect:true,
			fitColumns:true,
			method:"POST",
			nowrap:true,
			striped: true,
			collapsible:true,
			rownumbers:true,
			pagination:true,
			pageSize:30,
			height:450,
			width:880,
			columns:[[
				{field:'customerID',hidden:true},
				{field:'customerCode',title:'客户编号',width:100,align:"center"},
				{field:'customerName',title:'客户名称',width:100,align:"center"},
				{field:'customerTypeName',title:'客户类型',width:100,align:"center"},
				{field:'customerSourceName',title:'客户来源',width:100,align:"center"},
				{field:'roleName',title:'职位',width:100,align:"center"},
				{field:'telephone',title:'电话',width:100,align:"center"},
				{field:'address',title:'地址',width:100,align:"center"}
			]],
			onDblClickRow:function(rowIndex,rowData){
				onSelect();
			}
		});
	}
})(jQuery);